package com.ruoyi.web.controller.bus;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bus.domain.WitNotice;
import com.ruoyi.bus.service.IWitNoticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统通知Controller
 * 
 * @author caiyanshan
 * @date 2021-04-14
 */
@RestController
@RequestMapping("/bus/notice")
public class WitNoticeController extends BaseController
{
    @Autowired
    private IWitNoticeService witNoticeService;

    /**
     * 查询系统通知列表
     */
    @PreAuthorize("@ss.hasPermi('bus:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(WitNotice witNotice)
    {
        startPage();
        List<WitNotice> list = witNoticeService.selectWitNoticeList(witNotice);
        return getDataTable(list);
    }

    /**
     * 导出系统通知列表
     */
    @PreAuthorize("@ss.hasPermi('bus:notice:export')")
    @Log(title = "系统通知", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WitNotice witNotice)
    {
        List<WitNotice> list = witNoticeService.selectWitNoticeList(witNotice);
        ExcelUtil<WitNotice> util = new ExcelUtil<WitNotice>(WitNotice.class);
        return util.exportExcel(list, "notice");
    }

    /**
     * 获取系统通知详细信息
     */
    @PreAuthorize("@ss.hasPermi('bus:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Long noticeId)
    {
        return AjaxResult.success(witNoticeService.selectWitNoticeById(noticeId));
    }

    /**
     * 新增系统通知
     */
    @PreAuthorize("@ss.hasPermi('bus:notice:add')")
    @Log(title = "系统通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WitNotice witNotice)
    {
        return toAjax(witNoticeService.insertWitNotice(witNotice));
    }

    /**
     * 修改系统通知
     */
    @PreAuthorize("@ss.hasPermi('bus:notice:edit')")
    @Log(title = "系统通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WitNotice witNotice)
    {
        return toAjax(witNoticeService.updateWitNotice(witNotice));
    }

    /**
     * 删除系统通知
     */
    @PreAuthorize("@ss.hasPermi('bus:notice:remove')")
    @Log(title = "系统通知", businessType = BusinessType.DELETE)
	@DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        for(Long id : noticeIds){
            if(id.longValue() == 1l){//1是公告
                return AjaxResult.error("不可删除公告数据");
            }
        }
        return toAjax(witNoticeService.deleteWitNoticeByIds(noticeIds));
    }
}
